package com.miguo.component.search;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.miguo.ui.R;
import com.mrper.util.ApkUtil;

/**
 * File Names: SearchBarView
 * Describe:
 * User: LinXiaoZhi(87100854@qq.com)
 * Date: 2015-07-25
 * Time: 22:41
 */
public class SearchBarView extends LinearLayout {

    private Context mContext;

    private Paint mPaint;

    private EditText input_box;
    private View search_btn, back_btn, delete_txt, divider;


    public SearchBarView(Context context) {
        this(context, null);
    }

    public SearchBarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        this.mContext = context;
        setOrientation(HORIZONTAL);
        inflate(this.mContext, R.layout.layer_search_head, this);
        this.input_box = ((EditText) findViewById(R.id.input_box));
        this.search_btn = findViewById(R.id.search_btn);
        this.back_btn = findViewById(R.id.back_btn);
        this.delete_txt = findViewById(R.id.delete_txt);
        this.divider = findViewById(R.id.divider);
        this.mPaint = new Paint();
        this.mPaint.setAntiAlias(true);
        this.mPaint.setStyle(Paint.Style.FILL);
        //String hex = Integer.toHexString(-1447447);
        //this.mPaint.setColor(Color.parseColor("#ffe9e9e9"));
        //this.mPaint.setColor(Color.LTGRAY);
        this.mPaint.setColor(ApkUtil.getColorById(context,R.color.bg_divider_dddddd));
        this.mPaint.setStrokeWidth(1.0F);
        setWillNotDraw(false);
    }


    public EditText getEditText() {
        return this.input_box;
    }

    public void setDeleteVisibility(int visibility) {
        this.delete_txt.setVisibility(visibility);
        this.divider.setVisibility(visibility);
    }

    public void setSearchKeyListener(View.OnKeyListener listener) {
        this.input_box.setOnKeyListener(listener);
    }

    public void setSearchListener(View.OnClickListener listener) {
        if (this.input_box != null) {
            this.input_box.setOnClickListener(listener);
        }
    }

    public void setSearchChangedListener(TextWatcher listener) {
        if (this.input_box != null) {
            this.input_box.addTextChangedListener(listener);
        }
    }

    public void setSearchBtnListener(View.OnClickListener listener) {
        if (this.search_btn != null) {
            this.search_btn.setOnClickListener(listener);
        }
    }

    public String getKeyword() {
        if (this.input_box != null && this.input_box.getText() != null) {
            return this.input_box.getText().toString();
        }
        return null;
    }

    public void setKeyword(String keyword) {
        if (this.input_box != null) {
            this.input_box.setText(keyword);
        }
    }

    public void setBackListener(View.OnClickListener listener) {
        if (this.back_btn != null) {
            this.back_btn.setOnClickListener(listener);
        }
    }

    public void setDeleteListener(View.OnClickListener listener) {
        if (this.delete_txt != null) {
            this.delete_txt.setOnClickListener(listener);
        }
    }


    public void setHint(String hint) {
        if (this.input_box != null) {
            this.input_box.setHint(hint);
        }
    }

    public void setHint(int resource) {
        if (this.input_box != null) {
            this.input_box.setHint(resource);
        }
    }

    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawLine(0.0F, getHeight() - 1, getWidth(), getHeight() - 1, this.mPaint);
        setWillNotDraw(true);
    }

}
